package com.zhhtech.zhh.ui.widget

import android.app.Activity
import android.content.res.Configuration
import android.graphics.Point
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager.LayoutParams
import android.widget.PopupWindow
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LifecycleOwner
import com.zhhtech.zhh.R
import com.zhhtech.zhh.ui.ext.DefaultLifecycleObserver

class KeyboardHeightProvider(
    private val activity: FragmentActivity,
    private var observer: KeyboardHeightObserver? = null,
) : PopupWindow(activity), DefaultLifecycleObserver {

    private var keyboardLandscapeHeight: Int = 0

    private var keyboardPortraitHeight: Int = 0

    private var popupView: View

    private var parentView: View

    private var onResume: Boolean = false

    init {
        val inflater: LayoutInflater = activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        popupView = inflater.inflate(R.layout.keyboard_popup_window, null, false);
        contentView = popupView
        softInputMode = LayoutParams.SOFT_INPUT_ADJUST_RESIZE or LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
        inputMethodMode = INPUT_METHOD_NEEDED
        parentView = activity.findViewById(android.R.id.content)
        width = 0
        height = LayoutParams.MATCH_PARENT
        popupView.viewTreeObserver.addOnGlobalLayoutListener(::handleOnGlobalLayout)
        activity.lifecycle.addObserver(this)
        parentView.post(::start)
    }

    private fun start() {
        if (!isShowing && parentView.windowToken != null) {
            setBackgroundDrawable(ColorDrawable(0))
            showAtLocation(parentView, Gravity.NO_GRAVITY, 0, 0)
        }
        notifyKeyboardHeightChanged(0, Configuration.ORIENTATION_PORTRAIT)
    }

    override fun onResume(owner: LifecycleOwner) {
        onResume = true
        start()
    }

    override fun onPause(owner: LifecycleOwner) {
        onResume = false
        start()
    }

    override fun onDestroy(owner: LifecycleOwner) {
        observer = null
        dismiss()
    }

    private fun handleOnGlobalLayout() {
        if (!onResume) {
            return
        }
        val screenSize = Point()
        activity.windowManager.defaultDisplay.getSize(screenSize)
        val rect = Rect()
        popupView.getWindowVisibleDisplayFrame(rect)
        val orientation = getScreenOrientation()
        val keyboardHeight: Int = screenSize.y - rect.bottom
        if (keyboardHeight < 0 && observer != null) {
            observer?.mVirtualBottomHeight = -keyboardHeight
        }
        if (keyboardHeight == 0) {
            notifyKeyboardHeightChanged(0, orientation)
        } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            keyboardPortraitHeight = keyboardHeight
            notifyKeyboardHeightChanged(keyboardPortraitHeight, orientation)
        } else {
            keyboardLandscapeHeight = keyboardHeight
            notifyKeyboardHeightChanged(keyboardLandscapeHeight, orientation)
        }
    }

    private fun getScreenOrientation() = activity.resources.configuration.orientation

    private fun notifyKeyboardHeightChanged(height: Int, orientation: Int) {
        observer?.onKeyboardHeightChanged(height, orientation)
    }
}

abstract class KeyboardHeightObserver {
    var mVirtualBottomHeight: Int = 0

    abstract fun onKeyboardHeightChanged(height: Int, orientation: Int)
}